--news = {}

-- Modified by Tronex: 2019/4/22

--[[
tips_icons = {
	default  = { 0, 658},      -- ui_iconsTotal_grouping
	trader	 = { 332, 893},    -- ui_iconsNpc_trader
	dolg     = { 0, 658},
	freedom  = { 0, 658},
	ecolog   = { 498, 0},      -- ui_iconsNpc_ecolog
	army     = { 332, 141},    -- ui_iconsNpc_stalker_neytral_balon_6
	stalker  = { 0, 658},
	csky  	 = { 0, 658},
	krot     = { 332, 47},     -- ui_iconsNpc_stalker_neytral_rukzak_4
	barman   = { 332, 235},    -- ui_iconsNpc_barman
	wolf	 = { 332, 940},    -- ui_iconsNpc_stalker_neytral_balon_1
	o_soznanie = { 498, 893},
	monolith = { 0, 658},
	saharov  = { 332, 470},    -- ui_iconsNpc_ucheniy_2
	prizrak  = { 0, 658},
	killer   = { 0, 658},
	bandit   = { 0, 658},
	renegade = { 0, 658}
}
]]

tips_icons = {}
function on_game_start()
	local ini = ini_file("plugins\\news_tips_icons.ltx")
	local line_count = ini:line_count("news_tips_icons") or 0
	for k = 0, line_count - 1 do
		local junk1, section, value = ini:r_line("news_tips_icons", k, "", "")
		tips_icons[section] = value
	end
end


function send_tip(actor, news_id, timeout, sender, showtime, sender_id)
	if news_id == nil then return false end
	
	timeout = timeout or 0
	showtime = showtime or 5000

	--printf("try to send tips for [%s]", tostring(sender_id))
	if sender_id ~= nil then
		local id = get_story_object_id(sender_id)
		local npc = id and alife_object(id)
		if npc then
			if npc.online then
				if xr_wounded.is_heavy_wounded_by_id(npc.id) then
					printf("[PDA]Cannot send tips [%s], sender is heavy wounded", tostring(news_id))
					return false
				end
			end
			if not npc:alive() then
				printf("[PDA]Cannot send tips [%s], sender is dead", tostring(news_id))
				return false
			end
		end
	end


	--' Играем дефолтный звук
	xr_sound.set_sound_play(AC_ID, "pda_tips")
	
	-- play script sound with matching section name to news id string
	if (sound_theme.theme[news_id]) then
		xr_sound.set_sound_play(AC_ID, news_id)
		if (sound_theme.theme[news_id].snd_obj) then
			local length = sound_theme.theme[news_id].snd_obj:length()
			showtime = showtime < length and length or showtime
		end
	end

	local texture = "ui_iconsTotal_grouping"
	if sender ~= nil then
		if type(sender) == "string" then
			if tips_icons[sender] then
				texture = tips_icons[sender]
			end
 		elseif is_npc_stalker(sender:clsid()) then
			texture = sender:character_icon()
		end
	end
	local news_caption = game.translate_string("st_tip")
	local news_text = game.translate_string(news_id)
--	actor:give_game_news(news_caption, news_text, texture, timeout*1000, showtime)
	actor:give_game_news(news_caption, news_text, texture, timeout*1000, showtime, 0)

	return true
end


function send_sound(npc, faction, point, str, str2, delay_sound)
	--callstack()
	if faction ~= nil then
		--printf("---------------------")
		--printf(tostring(faction))
		local point_name = ""
		if point ~= nil then
			local smart = sim_board.get_sim_board().smarts[point]
			if smart then
				point_name = smart_terrain.get_smart_terrain_name(smart.smrt)
			else
				point_name = game.translate_string(point)
			end
		end
		--printf(point_name)
		local txt = ""
		if str2 == nil then
			txt = string.gsub(str, "(characters_voice\\human_..\\)([^\\]*)", "%2")
			txt = string.gsub(txt, "[\\]([^\\]*)", "_%1")
		else
			txt = str2
		end
		--printf("--->"..tostring(txt))

		local news_text = game.translate_string(txt)
		if news_text == txt then
			return
		end


--		callstack()
		--' Играем дефолтный звук
--	    xr_sound.set_sound_play(AC_ID, "pda_tips")

		local texture = "ui_iconsTotal_grouping"
		if npc ~= nil and is_npc_stalker(npc:clsid()) then
			texture = npc:character_icon()
		else
			if faction and tips_icons[faction] then
				texture = tips_icons[faction]
			end
			if point and tips_icons[point] then
				texture = tips_icons[point]
			end
		end
--		printf("--->texture="..tostring(texture))
		local news_caption = game.translate_string("st_tip").." "..game.translate_string(faction)
		if point_name ~= "" then
			news_caption = news_caption..". "..point_name..":"
		else
			news_caption = news_caption..":"
		end


--		db.actor:give_game_news(news_caption, news_text, texture, 1000, 5000)
--		give_game_news(LPCSTR caption, LPCSTR news, LPCSTR texture_name, int delay, int show_time)
		db.actor:give_game_news(news_caption, news_text, texture, delay_sound+1000, 5000, 1)

		return true

	end
end

function is_npc_stalker(class_id)
	if (class_id == clsid.script_stalker) or (class_id == clsid.stalker) then
		return true
	end
	return false
end


local action_descr_by_type = {
	new = "general_new_task",
	complete = "general_complete_task",
	fail = "general_fail_task",
	reversed = "general_reverse_task",
	updated = "general_update_task"
}
function send_task(actor, type, tsk)
	if db.actor == nil then
		return false
	end
	local time_on_screen = (type == "updated") and 5000 or 10000
	xr_sound.set_sound_play(AC_ID, "pda_task")
	local news_caption = game.translate_string(action_descr_by_type[type])
	local news_text = game.translate_string(tsk:get_title() or "")
	local icon = tsk:get_icon_name()
	if(icon==nil) then
		icon = "ui_iconsTotal_storyline"
	end

	if db.actor:is_talking() then
		db.actor:give_talk_message2(news_caption, news_text..".", icon, "iconed_answer_item")
	else
		db.actor:give_game_news(news_caption, news_text..".", icon, 0, time_on_screen)
	end

end


local showcase_treasure_caption = {
	[0] = "st_found_new_treasure",
	[1] = "st_got_treasure",
	[2] = "st_found_old_treasure",
}
function send_treasure(param)
	local news_caption = param and game.translate_string(showcase_treasure_caption[param]) or ""
	if db.actor:is_talking() then
		db.actor:give_talk_message2(news_caption, "", "ui_inGame2_Polucheni_koordinaty_taynika", "iconed_answer_item")
	else
		db.actor:give_game_news(news_caption, "", "ui_inGame2_Polucheni_koordinaty_taynika", 0, 3000)
	end
	
	xr_sound.set_sound_play(AC_ID, "pda_tips")
end


local showcase_item_icon = {
	["in"] = "ui_inGame2_Predmet_poluchen",
	["out"] = "ui_inGame2_Predmet_otdan",
	["docs"] = "ui_inGame2_Poslednie_razrabotki",
}
local showcase_item_caption = {
	["in"] = "general_in_item",
	["out"] = "general_out_item",
}
function relocate_item(actor, type, item, amount)
	if db.actor == nil then return false end
	if not (item and ini_sys:section_exist(item)) then 
		printe("!ERROR: relocate_item - section %s does not exist",item)
		return false
	end

	amount = amount or 1
	local news_text = item and ui_item.get_sec_name(item) or ""
	local news_caption = showcase_item_caption[type] and game.translate_string(showcase_item_caption[type]) or ""
	local news_ico = showcase_item_icon[type] or "ui_inGame2_Predmet_poluchen"
	if (amount > 1) then
		news_text = news_text .. " x" .. amount
	end
	
	if db.actor:is_talking() then
		db.actor:give_talk_message2(news_caption, news_text, news_ico, "iconed_answer_item")
	else
		db.actor:give_game_news(news_caption, news_text, news_ico, 0, 3000)
	end
end


local showcase_money_icon = {
	["in"] = "ui_inGame2_Dengi_polucheni",
	["out"] = "ui_inGame2_Dengi_otdani",
}
local showcase_money_caption = {
	["in"] = "general_in_money",
	["out"] = "general_out_money",
}
function relocate_money(actor, type, amount)
	if db.actor == nil then return false end
	
	local news_text = game.translate_string(tostring(amount))
	local news_caption = showcase_money_caption[type] and game.translate_string(showcase_money_caption[type]) or ""
	local news_ico = showcase_money_icon[type] or "ui_inGame2_Dengi_polucheni"
	
	if db.actor:is_talking() then
		db.actor:give_talk_message2(news_caption, news_text, news_ico, "iconed_answer_item")
	else
		db.actor:give_game_news(news_caption, news_text, news_ico, 0, 3000)
	end
end